package com.itic.codeGen.codeGenerator.workLine.implement;

import java.util.ArrayList;

import com.itic.codeGen.codeGenerator.util.CommonUtils;
import com.itic.codeGen.codeGenerator.util.CommonUtils.FILE_TYPE;
import com.itic.codeGen.codeGenerator.workLine.PartGenerator;

public class ControllerGenerator implements PartGenerator {
    private static final String AUTOWIRE = "org.springframework.beans.factory.annotation.Autowired";
    private static final String CONTROLLER = "org.springframework.stereotype.Controller";
    private static final String REQUESTMAPPING = "org.springframework.web.bind.annotation.RequestMapping";
    private static final String BaseController = "com.itic.appbase.framework.common.controller.BaseController";
    private static final String Annotation = "@Controller";
    private static final String Annotation_2 = "@RequestMapping";
    private static final String Annotation_3 = "@Autowired";

    private String packageName;
    private String domainName;
    private ArrayList<String> imports = new ArrayList<String>();
    private ArrayList<String> headers = new ArrayList<String>();
    private String extend;
    private ArrayList<String> body = new ArrayList<String>();

    public ControllerGenerator(String packageName, String domainName) {
	super();
	this.packageName = packageName;
	this.domainName = domainName;
    }

    @Override
    public ArrayList<String> getImports() {
	imports.add(AUTOWIRE);
	imports.add(CONTROLLER);
	imports.add(REQUESTMAPPING);
	imports.add(BaseController);
	String serviceName = domainName + FILE_TYPE.SERVICE.getType();
	String servicePath = CommonUtils.packageFomatter(packageName,
	        FILE_TYPE.SERVICE) + CommonUtils.PKG_SPLITER + serviceName;
	imports.add(servicePath);
	return imports;
    }

    @Override
    public ArrayList<String> getHeaders() {
	headers.add(Annotation);
	String uriPath = getLastPackageName(packageName, domainName);
	headers.add(Annotation_2 + "(value=\"/" + uriPath + "\")");
	return headers;
    }

    @Override
    public ArrayList<String> getImplement() {
	return null;
    }

    @Override
    public String getExtend() {
	extend = CommonUtils.getClzName(BaseController);
	return extend;
    }

    @Override
    public ArrayList<String> getBody() {
	body.add(Annotation_3);
	String serviceName = domainName + FILE_TYPE.SERVICE.getType();
	body.add("private " + serviceName + " " + CommonUtils.lowerFirst(serviceName) + ";");
	return body;
    }

    @Override
    public String getXMLName() {
	return null;
    }

    public String getPackageName() {
	return packageName;
    }

    public void setPackageName(String packageName) {
	this.packageName = packageName;
    }

    public String getDomainName() {
	return domainName;
    }

    public void setDomainName(String domainName) {
	this.domainName = domainName;
    }

    public void setImports(ArrayList<String> imports) {
	this.imports = imports;
    }

    public void setHeaders(ArrayList<String> headers) {
	this.headers = headers;
    }

    public void setExtend(String extend) {
	this.extend = extend;
    }

    public void setBody(ArrayList<String> body) {
	this.body = body;
    }

    /**
     * 
     * @Title: getLastPackageName
     * @param pkgName
     * @param domainName
     * @return
     * @throws
     */
    private String getLastPackageName(String pkgName, String domainName) {
	String[] arrys = pkgName.split("\\" + CommonUtils.PKG_SPLITER);
	if (arrys.length > 3) {
	    return arrys[arrys.length - 3];
	}
	return domainName;
    }

}
